<?php
class admin extends base{

	public function init(){ // 公用
		parent::init(); // 这是必须的

		$this->tit = "管理员";
		$this->modelObj = D('sys_admin');
		$this->roleObj = D('sys_role');

		$this->data_list = $this->roleObj->get_treeList("status=1");

	}

	//列表
	public function index(){
		$this->tabtit = array('全部','禁用');

		$cond = "1=1 and is_admin=0";
		//加入查询条件
		switch($this->tab){
			case '1':
				$cond .=" and status=0";
				break;
			default:
				$cond .=" and status=1";
		}
		//搜索
		if($this->keyword){
			$cond.=" and (username like '%".$this->keyword."%') ";
		}

		$result = $this->modelObj->get_pagelist($cond);
        if($result){
        	$this->roles = $this->roleObj->ids($result['list'],'role_id');
			// dump($this->roles);exit;
        	$this->list = $result['list'];
			$this->showPage = $result['showPage'];
			$this->total = $result['total'];
		}
		
	}

	//添加
	public function add(){
		if($this->isAjax()){
			$data = $this->postData('data');
			$res = $this->modelObj->check_user($data['username']);
			if($res){
				$this->renderError('用户名已存在');
			}

			$data['add_time'] = date('Y-m-d H:i:s');
			$this->modelObj->add($data);
			$this->renderSuccess('添加成功', "/{$this->routeUrl}/index");
		}
		$this->info = array('status'=>1);
		// dump($this->accessList);exit;
		$this->display($this->routeUrl."/set.php");
	}

	//编辑
	public function edit(){
		$id = arg('id');
		if($this->isAjax()){
			$data = $this->postData('data');
			$this->modelObj->edit($id,$data);
			$this->renderSuccess('编辑成功', "/{$this->routeUrl}/index");
		}
		
		$this->info = $this->modelObj->findById($id);
		// $cond = "status=1";
		// $this->cateList = $this->cateObj->getJsTree($cond,$this->info['cate_ids']);
		// $this->zoneList = $this->zoneObj->getJsTree($cond,$this->info['zone_ids']);
		// dump($this->cateList);exit;
		$this->display($this->routeUrl."/set.php");
	}

    //审核
	public function act()
	{
		$id = arg('id');
		$status = arg('status');
		$msg = ($status==1)?'显示':'隐藏';
		$opid = arg('opid');
		if($this->isAjax()){
			if($id){
				$res = $this->modelObj->act($id,$status);
				$this->renderSuccess($msg.'成功');
			}else{
				if($opid){
					foreach($opid as $val){
						$res = $this->modelObj->act($val,1);
					}
					$this->renderSuccess('显示成功');
				}else{
					$this->renderError('请选择数据');
				}
			}
		}
	}

	//隐藏
	public function ban()
	{
		$opid = arg('opid');
		if($this->isAjax()){
			if($opid){
				foreach($opid as $val){
					$res = $this->modelObj->act($val,0);
				}
				$this->renderSuccess('隐藏成功');
			}
			$this->renderError('请选择数据');
		}
	}

    //删除
	public function del()
	{
		$id = arg('id');
		$opid = arg('opid');
		if($this->isAjax()){
			if($id){
				$res = $this->modelObj->del($id);
				$this->renderSuccess('删除成功');
			}else{
				if($opid){
					foreach($opid as $val){
						$res = $this->modelObj->del($val);
					}
					$this->renderSuccess('删除成功');
				}else{
					$this->renderError('请选择数据');
				}
			}
			
		}
	}

	
	
	//排序
	public function sort()
	{
        $sort = arg('sort');
		$result = $this->modelObj->sort($sort);
		$this->renderSuccess('排序成功');
	}

	//管理
	public function login(){
		$id = arg('id');
		$cond = "id='{$id}' and status=1 ".$this->cond_base;
		$user = $this->modelObj->find(array($cond));

		$_SESSION['site_user']['id'] = $user['id'];
        $_SESSION['logintime'] = time();
        // set_cookie('company_admin_mnm',$user['username'],time()+3600*24);
		
		$this->redirect('/index/main/index');
	}

	// 修改密码
	public function pwd(){
		if($this->isAjax()){

			$data = $this->postData('data');
			$data['username'] = $this->admin['username'];
			if ($data['password'] !== $data['password_confirm']) {
				$this->renderError('确认密码不正确');
			}
	        $admin = $this->modelObj->pwd_user($data);
	        if($admin){
				// $_SESSION["admin"] = $admin;
	            $this->renderSuccess('更新成功', '/admin/login/logout');
				$this->redirect('/admin/login/logout');
	        }else{
	            $this->renderError('更新失败');
	        }
			
		}
	}

	//编辑
	public function auth(){
		$id = arg('id');
		if($this->isAjax()){
			$data = $this->postData('data');
			
			$arrBase['area_ids'] = joinStr($data['area']);
			$arrBase['cate_ids'] = joinStr($data['cate']);
			$arrBase['zone_ids'] = joinStr($data['zone']);

			$this->modelObj->edit($id,$arrBase);
			$this->renderSuccess('授权成功', "/{$this->routeUrl}/index");
		}
		$this->info = $this->modelObj->findById($id);

		$cond = "status=1".$this->cond_base;
		$this->areaList = $this->areaObj->get_jsTree($cond,$this->info['area_ids']);
		$this->cateList = $this->cateObj->get_jsTree($cond,$this->info['cate_ids']);
		$this->zoneList = $this->zoneObj->get_jsTree($cond,$this->info['zone_ids']);
	}


}